package com.winit.openapi.mms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.mms.spi.thirdcategory.ThirdPartyCategoryService;
import com.winit.mms.spi.vo.AttributeConfigThirdVo;
import com.winit.mms.spi.vo.ThirdPartyCategoryVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 
 * mms万邑通分类操作控制器
 * 
 * @version 
 * <pre>
 * Author   Version     Date        Changes
 * hao.gong     1.0         2015年5月18日  Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/mms/thirdCategory")
public class ThirdPartyCategoryController extends BaseController {
    /*
     * 万邑通分类服务
     */
    @Resource
    private ThirdPartyCategoryService thirdPartyCategoryService;
    
    @RequestMapping(value="/create")
    public String findWinitCategory(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        /*
         * <pre>1、获取请求参数</pre></br>
         */
        Object obj = requestMsg.getData();
        if (obj instanceof JSONArray) {
            JSONArray arr = (JSONArray) obj;
            
            for (int i = 0; i < arr.size(); i++) {
                JSONObject json = arr.getJSONObject(i);
                ThirdPartyCategoryVo vo = buildParams(json);
                thirdPartyCategoryService.addThirdPartyCategoryDetail(vo);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        return SUCCESS;
    }
    
    /**
     * 构建请求参数
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
    private ThirdPartyCategoryVo buildParams(JSONObject json){
        ThirdPartyCategoryVo vo = new ThirdPartyCategoryVo();
        
        /**
         * 分类代码
         * code
         */
        if (json.containsKey("code")) {
            vo.setCode(json.getString("code").trim());
        }
        
        /**
         * 分类名称
         * name
         */
        if (json.containsKey("name")) {
            vo.setName(json.getString("name").trim());
        }
        
        /**
         * 父级代码
         * parentCode
         */
        if (json.containsKey("parentCode")) {
            vo.setParentCode(json.getString("parentCode").trim());
        }
        
        /**
         * 层级
         * level
         */
        if (json.containsKey("level")) {
            vo.setLevel(json.getInteger("level"));
        }
        
        /**
         * 碰头类型
         * platFormType
         */
        if (json.containsKey("platFormType")) {
            vo.setPlatFormType(json.getString("platFormType").trim());
        }
        
        /**
         * 平台
         * specification
         */
        if (json.containsKey("platForm")) {
            vo.setPlatForm(("platForm").trim());
        }
        JSONArray attributes = json.getJSONArray("lstAttribute");
        if(!attributes.isEmpty()){
            List<AttributeConfigThirdVo> list = new ArrayList<AttributeConfigThirdVo>();
            for(Object object : attributes){
                Map<String, Object> map = (Map<String, Object>) object;
                AttributeConfigThirdVo attributeVo  = new AttributeConfigThirdVo();
                attributeVo.setCategoryId(Integer.valueOf((String) map.get("categoryId")));
                attributeVo.setIsParent(false);
                attributeVo.setAttributeId(Integer.valueOf((String) map.get("attributeId")));
                attributeVo.setAttributeSetId(Integer.valueOf((String) map.get("attributeSetId")));
                attributeVo.setIsNecessary(Boolean.valueOf((String) map.get("isNecessary")));
                list.add(attributeVo);
            }
            vo.setAttributeConfigThirdList(list);
        }
        return vo;
    }
}
